package cn.itcast.comsumer;

import com.rabbitmq.client.*;

import java.io.IOException;

public class Comsumer_routing1 {
    private static final String QUEUE2="pubsub_baojing";

    public static void main(String[] args) {
        try {
            //rabbitmq连接
            ConnectionFactory factory = new ConnectionFactory();
            factory.setHost("192.168.200.128");
            factory.setPort(5672);
            factory.setUsername("guest");
            factory.setPassword("guest");
            factory.setVirtualHost("/");

            //建立长连接
            Connection connection = factory.newConnection();

            //建立通道
            Channel channel = connection.createChannel();

            //接收消息
            //String 队列名,
            //boolean 自动应答  队列收到消息，删除,
            //Consumer callback 回调函数
            Consumer consumer = new DefaultConsumer(channel){
                /**
                 * @param consumerTag 消费者标签
                 * @param envelope  信封
                 * @param properties 参数
                 * @param body  byte[] 消息
                 * @throws IOException
                 */
                @Override
                public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                    //业务逻辑
                    System.out.println(consumerTag);
                    System.out.println(envelope);
                    System.out.println(properties);
                    System.out.println(new String(body,"utf-8"));
                }
            };
            channel.basicConsume(QUEUE2,true,consumer);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
